package cc.lishixi.book.service;

import cc.lishixi.book.dao.mapper.LibrarianRowMapper;
import cc.lishixi.book.entity.Librarian;
import cc.lishixi.book.service.api.AbstractService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by LiShixi on 2016/7/24 0024.
 * http://www.lishixi.cc
 */
@Service
public class LibrarianService extends AbstractService<Librarian> {
    @Override
    public List<Librarian> list() {
        return jdbcTemplate.query("select * from librarian", new LibrarianRowMapper());
    }

    public Librarian get(String userid) {

        try {

            String sql = "select * from librarian where userid=?";
            List<Librarian> query = jdbcTemplate.query(sql, new LibrarianRowMapper(), userid);
            if(!query.isEmpty()){
                return query.get(0);
            }else{

                log.warning("SQL=[select * from librarian where userid='"+userid+"'] result=size is 0!");
            }


        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return new Librarian();
    }

    @Override
    public boolean delete(Integer id) {
        return jdbcTemplate.update("delete from librarian where id=?", id) > 0;
    }

    @Override
    public boolean edit(Librarian l) {
        String sql = "UPDATE `book`.`librarian` SET `passwd`=?, `name`=?, `bookp`=?, `readerp`=?, `parameterp`=? WHERE  `id`=?;";
        return jdbcTemplate.update(sql, l.getPasswd(), l.getName(), l.getBookp(), l.getReaderp(), l.getParameterp(), l.getId()) > 0;
    }

    @Override
    public boolean add(Librarian l) {
        String sql = "INSERT INTO `book`.`librarian` (`userid`,`passwd`, `name`, `bookp`, `readerp`, `parameterp`) VALUES (?,?,?, ?, ?, ?, ?);";
        return jdbcTemplate.update(sql, l.getUserid(), l.getPasswd(), l.getName(), l.getBookp(), l.getReaderp(), l.getParameterp()) > 0;
    }
}
